package com.infinit.easyreader.Biz;

import android.util.Log;

import com.infinit.easyreader.utils.Consts;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 类：com.infinit.easyreader.Biz.OnlineBookBiz V[1.0.0]
 * 作用：网上书城的处理类
 * Created by rzy on 2016/6/1.
 */
public class OnlineBookBiz implements Consts {
    private OkHttpClient mOkHttpClient;
    private HashMap<String, String> params;

    public OnlineBookBiz() {
        this.mOkHttpClient = new OkHttpClient();
    }

    public void testCollection() {

        final Request request = new Request.Builder().url(url).build();

        //一个新的call
        Call call = mOkHttpClient.newCall(request);
        //将call加入到调度中
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: 请求数据失败了");

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String rpsString = response.body().string();
                try {
                    JSONObject object = new JSONObject(rpsString);
                    Log.d(TAG, "onResponse: " + object.getString("reason"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });


    }


    /**
     * 链接到聚合数据接口
     *
     * @param method 请求方式
     * @param params 请求参数
     * @return 返回的数据实体
     */
    public JSONObject connAPI(String method, HashMap<String, String> params, String URL) {
        String url;



        //发送请求


        return null;


    }


    private String urlencode(Map<String, String> data) {
        StringBuilder builder = new StringBuilder();
        for (Map.Entry i : data.entrySet()) {
            try {
                builder.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return builder.substring(0, builder.length());
    }


}
